﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using EasyToHelp.Common;
using Microsoft.AspNetCore.Mvc;
using UploadHandle;

namespace EasyToHelp.WAP.Controllers
{
    public class CommonController : Controller
    {
        public IActionResult UploadImgFile()
        {
            var ImagePath = Request.Form.Files[0];
            string UploadPath = "/Uploads/Images/" + string.Format("{0:yyyyMMdd}", DateTime.Now) + "/";
            string path = $"{ Directory.GetCurrentDirectory() }/wwwroot" + UploadPath;
            //获取文件后缀名
            string filepath = Path.GetExtension(ImagePath.FileName);
            //判断路径是否存在，不存在则创建
            if (!Directory.Exists(path))
            {
                Directory.CreateDirectory(path);
            }
            //判断后缀是否是图片
            const string fileFilt = ".gif|.jpg|.jpeg|.png|.GIF|.JPG|.JPEG|.PNG|";
            if (fileFilt.IndexOf(filepath.ToLower(), StringComparison.Ordinal) <= -1)
            {
                return Json(new { code = 0, msg = "请上传图片格式文件！" });
            }
            //创建文件名
            var fileName = EncryptionMD5.CreateMd5(Guid.NewGuid().ToString()) + filepath;
            using (FileStream fs = System.IO.File.Create(path + fileName))
            {
                ImagePath.CopyTo(fs);
                fs.Flush();
            }
            return Json(new { code = 1, msg = "上传成功！", file = UploadPath + fileName });
        }
        /*********Ajax 方式使用**********/
        public void Ajax()
        {
            AjaxReceiver _receive = new AjaxReceiver(this.HttpContext);
            //接收文件成功
            _receive.OnSuccess = (data) =>
            {
                // Write(string.Format("新文件名{0},旧文件名{1}", data.NewName, data.OldName));
            };
            _receive.DoWork();
        }
        public void Ajax_Thumb()
        {
            try
            {
                AjaxReceiver _receive = new AjaxReceiver(this.HttpContext, "imgsouce/origin");
                //接收文件成功
                _receive.OnSuccess = (data) =>
                {
                    //接收文件成功后，自动生成缩略图
                    // 大图
                    ThumbnailHandle _thumb = new ThumbnailHandle(data, "big", 920);
                    _thumb.AutoHandle();
                    string big = _thumb.GetRelativeName();
                    // Write("大图位置：" + big);

                    //小图
                    _thumb.Width = 320;
                    _thumb.Folder = "small";
                    _thumb.AutoHandle();
                    string small = _thumb.GetRelativeName();
                    // Write("小图位置：" + small);

                    data.Data = new { big = big, small = small, oldname = data.OldName };

                    //此处，有需要的情况下，执行数据库操作
                    // Write(string.Format("新文件名{0},旧文件名{1}", data.NewName, data.OldName));
                };
                _receive.DoWork();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        /// <summary>
        /// 下载文件
        /// </summary>
        /// <param name="url"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult Export(string url)
        {
            FileStream fs = new FileStream(url, FileMode.OpenOrCreate);
            StreamWriter sw = new StreamWriter(fs);
            sw.Flush();
            sw.Close();
            fs.Close();
            return File(new FileStream(url, FileMode.Open), "application/octet-stream", $"导出测试{DateTime.Now.ToString("yyyyMMddHHmmss")}.txt");
        }
    }
}